import javax.swing.*;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import java.awt.*;

public class Peg extends JPanel {

	// four different color buttons, along with the empty peg iamge
	ImageIcon yellow;
	ImageIcon blue;
	ImageIcon red;
	ImageIcon green;
	ImageIcon emptyicon = new ImageIcon("src/images/emptypeg.gif");
	JLabel label;

	Border empty = BorderFactory.createEmptyBorder();

	// the background color of the game board
	Color background = new Color(189, 186, 189);

	int state;

	public Peg() {
		super();
		label = new JLabel(emptyicon);
		this.setBorder(empty);
		this.setBackground(background);
		this.add(label);
		state = 0;
	}

	public void switchColor() {
		// rotates the pegs through the different colors
		// everytime the user clicks, it rotates to the next color
		// in the sequence: red, yellow, blue, green
		state++;
		if (state > 4)
			state = 1;
		if (state == 1)
			label = new JLabel(new ImageIcon("src/images/red.gif"));
		if (state == 2)
			label = new JLabel(new ImageIcon("src/images/yellow.gif"));
		if (state == 3)
			label = new JLabel(new ImageIcon("src/images/blue.gif"));
		if (state == 4)
			label = new JLabel(new ImageIcon("src/images/green.gif"));

		this.removeAll();
		this.repaint();
		this.add(label);
		this.validate();
	}

	// returns the color that is currently showing
	public int getState() {
		return state;
	}

	// set the state to a particular color
	public void setState(int value) {
		this.state = value;
		if (state == 0)
			label = new JLabel(new ImageIcon("src/images/emptypeg.gif"));
		if (state == 1)
			label = new JLabel(new ImageIcon("src/images/red.gif"));
		if (state == 2)
			label = new JLabel(new ImageIcon("src/images/yellow.gif"));
		if (state == 3)
			label = new JLabel(new ImageIcon("src/images/blue.gif"));
		if (state == 4)
			label = new JLabel(new ImageIcon("src/images/green.gif"));

		this.removeAll();
		this.repaint();
		this.add(label);
		this.validate();
	}
}